/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import org.ukui.tablet 1.0
import org.ukui.qqc2style.private 1.0 as StylePrivate
import "../baseComponent/"
import "../launcherController.js" as Controller

LauncherGridViewDelegateItem {
    id: itemRoot
    width: tabletRoot.iconSize
    height: tabletRoot.iconSize
    anchors.topMargin: (modelData.columnSpan * tabletRoot.cellWidth - iconSize)/2 * Style.tabletScaleFactor

    property int itemId: modelData.id
    property bool mouseReleased: false
    property int type: modelData.type
    property var dragAppIcon: item
    property var dragAppName: itemName
    property var itemRootDragMouseArea: dragMousearea
    property int groupId: tabletRoot.groupId

    states: [
        State {
            when: itemRoot.Drag.active
            ParentChange {
                target: itemRoot
                parent: topLayer
            }

            AnchorChanges {
                target: itemRoot
                anchors.horizontalCenter: undefined
                anchors.top: undefined
            }

            PropertyChanges {
                target: item
                iconState: LauncherItem.Dragging
                iconWidth: 96 * Style.tabletScaleFactor * 1.1
            }

            PropertyChanges {
                target: itemRoot
                width: 96 * Style.tabletScaleFactor * 1.1
                height: 96 * Style.tabletScaleFactor * 1.1
            }
        }
    ]

    MouseArea {
        id: dragMousearea
        anchors.fill: parent
        hoverEnabled: false
        pressAndHoldInterval: modelData.state === LauncherItem.Editable ?
                                  100 : Style.pressIntervalToEditable
        property bool isScrolling: false
        property bool isChangeListviewPage: false
        property bool groupHeightChanged: false
        property point startDragPoint: Qt.point(0, 0)

        Timer {
            id: changeListviewPage
            interval: 600
            onTriggered: {
                dragMousearea.isChangeListviewPage = true
            }
        }
        Timer {
            id: itemToDesktopTimer
            interval: 600
            onTriggered: {
                GroupController.minimizeGroup()
                launcherItemToDesktop()
                itemToDesktopTimer.stop()
            }
        }

        onPressAndHold: {
            if (modelData.type === LauncherItem.AddIcon) {
                return;
            }
            startDrag(true)
            modelManager.setAllItemsState(LauncherItem.Editable)
            dragMousearea.Drag.active = true
            dragMousearea.drag.target = itemRoot
            startDragPoint = mapToGlobal(itemRoot.x, itemRoot.y)
            console.log("startDragPoint", startDragPoint)
        }

        onClicked: {
            if (modelData.type === LauncherItem.AddIcon) {
                showAddAppWindow(tabletRoot.groupId, modelData.pageIndex)
            } else if (modelData.type === LauncherItem.AppIcon && modelData.state === LauncherItem.Normal) {
                appManager.runApplication(modelData.desktopFile)
            }
        }
        onPositionChanged:  {
            Controller.tabletAppItemPositionChanged(dragMousearea,
                                              changeListviewPage,
                                              itemToDesktopTimer,
                                              item,
                                              itemRoot,
                                              GroupController.groupContainer,
                                              GroupController.groupContainerId)
        }
        onPressed: {
            if (modelData.type === LauncherItem.AppIcon) {
                itemRoot.mouseReleased = false
            }
            if (modelData.type === LauncherItem.AddIcon) {
                iconpressAnimation.target = item
                iconpressAnimation.start()
            }
        }

        onReleased: {
            console.log("Launcher item view release.")
            itemToDesktopTimer.stop()
            itemRoot.mouseReleased = true
            if (modelData.type === LauncherItem.AddIcon) {
                iconreleseAnimation.target = item
                iconreleseAnimation.start()
                return
            }
            startDrag(false)
            isScrolling = false
            dragMousearea.Drag.active = false
            dragMousearea.drag.target = null
            changeGroupHeight()
            groupHeightChanged = false
            if (GroupController.aboutToInsertGroup && GroupController.aboveItem !== null &&
                    GroupController.aboveItem.groupId !== GroupController.popupedGroupId) {
                GroupController.insertItemToGroup(GroupController.aboveItem.visualIndex, GroupController.aboveItem.pageIndex,
                                                  GroupController.aboveItem.itemId, GroupController.aboveItem.dragAppIcon,
                                                  GroupController.aboveItem.dragAppName)
                modelManager.updateCachedData(GroupController.popupedGroupId)
            } else if (taskbar.replayAllowed) {
                if (modelData.type === LauncherItem.AppIcon) {
                    if (modelManager.replacePlaceholderItemTaskbar(visualIndex, pageIndex, groupId, 0)) {
                        modelManager.updateCachedData(groupId)
                    }
                }
                taskbar.replayAllowed = false
            } else {
                modelManager.replacePlaceholderItem(itemRoot.visualIndex, itemRoot.pageIndex, groupId)
            }
            modelManager.updateCachedData()
            modelManager.updateCachedData(0)
        }
    }

    TabletAppIcon {
        id: item
    }
    GridViewDelegateItemText {
        id: itemName
        width: item.width + 40 * Style.tabletScaleFactor
        anchors { top: item.bottom; topMargin: 8 * Style.tabletScaleFactor
                  horizontalCenter: item.horizontalCenter }
        font: StylePrivate.StyleHelper.font
        visible: modelData.type !== LauncherItem.Placeholder &&
                 modelData.nameVisible &&
                 item.iconState !== LauncherItem.Dragging
    }
}
